package com.ruoyi.file.utils;

import cn.hutool.core.codec.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Random;

/**
 1) 加密算法：PBEWithMD5AndDES
 2) 迭代次数：1000
 */
public class FileEnBase64 {

    public static void Encryption (String s,String s2) throws Exception {
        byte ptext[]=s.getBytes("UTF8");
        FileOutputStream f=new FileOutputStream (s2 ) ;
        f.write (ptext);
    }
    public static void Decryption (String str2,String str3) throws Exception {
        FileInputStream f=new FileInputStream(str2);
        int num=f.available();
        byte[ ] ctext=new byte[num];
        f.read(ctext);
        System.out.println ("解密结果为：") ;

        FileOutputStream f2=new FileOutputStream (str3) ;
        f2.write(Base64.decode(ctext));

    }



    public static String Read (String str1)							//读取待加密文件
    {
        File file=new File(str1);
        try{
            FileInputStream fileInput=new FileInputStream(file);
            try {
                String encode = Base64.encode(fileInput);
                return encode;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
            System.out.println("文件不存在或者文件不可读或者文件是目录");
        }
        return null;
    }
}
